<?php
/**
 * User: hjd-yao
 * Date: 2019/5/9
 * Time: 12:54
 */

namespace App\Http\Controllers\User;


use App\Helpers\Search;
use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\PlatFinanceModel;
use App\Models\Store\ShopStoreModel;
use App\Models\Store\ShopStoreWdModel;
use App\Models\User\AccountFlowModel;
use App\Models\User\WdModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserBaseModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Http\Request;


class WdController extends BackendController
{

    /**
     * @title 提现记录
     * @param Request $request
     * @return View
     */
    public function wdRecord(Request $request)
    {
        $search = Search::make($request)->bindModel(WdModel::with('user')->has('user'))
            ->input('user','编号/姓名/手机号','',['field'=>'user_id'])
            ->time('created_at','申请时间')
            ->bindSearchData()->relationModel(['user'=>function($value){
                return UserBaseModel::where('user_number',$value)
                    ->orWhere('real_name',$value)->orWhere('user_phone',$value)->pluck('member_id')->toArray();
            }]);
        $list = $search->where()->whereIn('status',[-1,1])->orderBy('created_at','desc')
            ->paginate($this->getPerPageNum());

        return view('backend::wd.wd_record',[
            'page_title'=>'提现记录',
            'list'=>$list,
            'search'=>$search
        ]);
    }

    /**
     * @title 提现申请
     * @param Request $request
     * @return View
     */
    public function wdApply(Request $request)
    {
        $search = Search::make($request)->bindModel(WdModel::with('user')->has('user'))
            ->input('user','编号/姓名/手机号','',['field'=>'user_id'])
            ->time('created_at','申请时间')
            ->bindSearchData()->relationModel(['user'=>function($value){
                return UserBaseModel::where('user_number',$value)
                    ->orWhere('real_name',$value)->orWhere('user_phone',$value)->pluck('member_id')->toArray();
            }]);

        $list = $search->where()->where('status',0)
            ->paginate($this->getPerPageNum());

        return view('backend::wd.wd_apply',[
            'page_title'=>'提现申请',
            'list'=>$list,
            'search'=>$search
        ]);
    }

    /**
     * @title 提现详情
     * @param $wd_id
     * @return View
     */
    public function wdDetail($wd_id)
    {
        $info = WdModel::find($wd_id);
        return view('backend::wd.wd_detail',[
            'page_title'=>'提现详情',
            'info'=>$info
        ]);
    }

    /**
     * @title 删除提现记录
     * @param Request $request
     * @return SelfResponse
     */
    public function wdDelete(Request $request)
    {
        $ids= $request->input('wd_id');
        $ids = array_filter(is_array($ids)?$ids:str2arr($ids));
        if(!$ids){
            return SelfResponse::error('请选择要操作的数据');
        }
        if(WdModel::whereIn('status',[-1,1])->whereIn('id',$ids)->delete()){
            return SelfResponse::success('删除成功','b_Wd_wdRecord');
        }
        return SelfResponse::error('删除失败');
    }

    /**
     * @title 同意提现
     * @param $wd_id
     * @param Request $request
     * @return SelfResponse
     */
    public function wdAgree(Request $request,$wd_id)
    {
        if($request->isMethod('post')){
            $info = WdModel::find($wd_id);
            if(!$info||$info->status!=0){
                return SelfResponse::error('该提现记录已处理');
            }
            $account = ["类型"=>$request->post('account_type')];

            $account['账号'] = $request->post('card');
            if($request->post('bank_name')){
                $account['开户行'] = $request->post('bank_name');
            }
            $info->status = 1;
            $info->from_account = json_encode($account);
            $info->from_phone = $request->post('phone');
            $info->handle_remark = $request->post('remark','已汇款');
            $info->handled_at = Carbon::now()->timestamp;

            if($info->save()){
                PlatFinanceModel::init()->incomeFinance($info->service_fee);
                return SelfResponse::success('提现审批成功','b_Wd_wdRecord');
            }
            return SelfResponse::error('审批失败');
        }else{
            return view('backend::wd.wd_agree',[
                'page_title'=>'汇款账户信息',
                'wd_id'=>$wd_id,
            ]);
        }
    }

    /**
     * @title 拒绝提现
     * @param Request $request
     * @param $wd_id
     * @return SelfResponse
     */
    public function wdRefuse(Request $request, $wd_id)
    {
        $info = WdModel::find($wd_id);
        if(!$info||$info->status!=0){
            return SelfResponse::error('该提现记录已处理');
        }
        $info->status = -1;
        $info->handle_remark = $request->input('remark','拒绝提现');
        $info->handled_at = Carbon::now()->timestamp;
        $money = $info->money+$info->service_fee;
        if($info->save()){
            $wallet = UserWalletModel::find($info->user_id);
            if(!UserWalletModel::where('member_id',$info->user_id)->increment('money2',$money)){
                $info->status = 0;
                $info->handle_remark = '提现拒批';
                $info->save();
                return SelfResponse::error('拒批失败,请稍后再试');
            }
            AccountFlowModel::init()->createFlow($info->user_id,$money,1,[
                'before_money'=>$wallet->money2,
                'change_type'=>52,
                'relate_uid'=>0,
                'money_type'=>2,
                'comment'=>'提现驳回退款',
            ]);
            return SelfResponse::success('拒批成功,提现金额已退回','b_Wd_wdRecord');
        }
        return SelfResponse::error('拒批失败,请稍后再试');
    }
}